
#import "UITableView+Placeholder.h"
#import "NSObject+Swizzling.h"
#import "CTMediator+M1NoneViewActions.h"
#import "MJRefresh.h"

@implementation UITableView (Placeholder)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self m1_methodSwizzlingWithOriginalSelector:@selector(reloadData)
                               bySwizzledSelector:@selector(sure_reloadData)];
    });
}

- (void)sure_reloadData {
    [self m1_checkEmpty];
    [self sure_reloadData];
}

- (void)m1_checkEmpty {
    BOOL isEmpty = YES;//flag标示

    id <UITableViewDataSource> dataSource = self.dataSource;
    NSInteger sections = 1;//默认一组
    if ([dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        sections = [dataSource numberOfSectionsInTableView:self] - 1;//获取当前TableView组数
    }

    for (NSInteger i = 0; i <= sections; i++) {
        NSInteger rows = [dataSource tableView:self numberOfRowsInSection:i];//获取当前TableView各组行数
        if (rows) {
            isEmpty = NO;//若行数存在，不为空
        }
    }
    if (isEmpty && [self.m1_needPlace integerValue] > 0) {//若为空，加载占位图
        //默认占位图
        if (!self.m1_placeView) {
            [self m1_defaultPlaceView];
        }
        self.m1_placeView.hidden = NO;
        [self addSubview:self.m1_placeView];
    } else {//不为空，移除占位图
        self.m1_placeView.hidden = YES;
    }
}

- (void)m1_defaultPlaceView {

    if(!self.imgName){
        self.imgName = @"kongbai";
    }
    if(!self.noneTitle){
        self.noneTitle = @"空空如也";
    }
    if(!self.noneDesc){
        self.noneDesc = @"";
    }
    if(!self.btnButtonTitle){
        self.btnButtonTitle = @"";
    }
    if(!self.imgWidth){
        self.imgWidth = @(90);
    }
    if(!self.imgY){
        self.imgY = @(150);
    }
    if(!self.imgHeight){
        self.imgHeight = @(68);
    }
    UIView *placeholderView = [[CTMediator sharedInstance] m1_noneViewWithTitle:[self noneTitle]
                                                                                noneDes:[self noneDesc]
                                                                           noneBtnTitle:[self btnButtonTitle]
                                                                                  image:[self imgName]
                                                                              width:[[self imgWidth] floatValue]
                                                                             imgheight:[[self imgHeight] floatValue]
                                                                                  img_y:[[self imgY] floatValue]
                                                                                 height:self.frame.size.height
                                                                                  width:self.frame.size.width
                                                                                  block:^{
                                                                                      [self reload_action];
                                                                                  }];
    self.m1_placeView = placeholderView;
}

- (void)reload_action{

    if(self.block){
        self.block();
    }else if(self.mj_header){
        [self.mj_header beginRefreshing];
    }
}

- (UIView *)m1_placeView {
    return objc_getAssociatedObject(self, @selector(m1_placeView));
}

- (void)setM1_placeView:(UIView *)placeholderView {
    objc_setAssociatedObject(self, @selector(m1_placeView), placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)m1_needPlace {
    return objc_getAssociatedObject(self, @selector(m1_needPlace));
}

- (void)setM1_needPlace:(NSNumber *)needPlaceholder{
    objc_setAssociatedObject(self, @selector(m1_needPlace), needPlaceholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)noneDesc{
    return objc_getAssociatedObject(self, @selector(noneDesc));
}

- (void)setNoneDesc:(NSString *)noneDesc{
    objc_setAssociatedObject(self, @selector(noneDesc), noneDesc, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)noneTitle{
    return objc_getAssociatedObject(self, @selector(noneTitle));
}

- (void)setNoneTitle:(NSString *)noneTitle{
    objc_setAssociatedObject(self, @selector(noneTitle), noneTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)btnButtonTitle{
    return objc_getAssociatedObject(self, @selector(btnButtonTitle));
}

- (void)setBtnButtonTitle:(NSString *)btnButtonTitle{
    objc_setAssociatedObject(self, @selector(btnButtonTitle), btnButtonTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)imgName{
    return objc_getAssociatedObject(self, @selector(imgName));
}

- (void)setImgName:(NSString *)imgName{
    objc_setAssociatedObject(self, @selector(imgName), imgName, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

- (void (^)(void))block{

    return objc_getAssociatedObject(self, @selector(block));
}

- (void)setBlock:(void (^)(void))block{
    objc_setAssociatedObject(self, @selector(block), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSNumber *)imgWidth{
    return objc_getAssociatedObject(self, @selector(imgWidth));
}
- (NSNumber *)imgHeight{
    return objc_getAssociatedObject(self, @selector(imgHeight));
}

- (void)setImgWidth:(NSNumber *)imgWidth{
    objc_setAssociatedObject(self, @selector(imgWidth), imgWidth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setImgHeight:(NSNumber *)imgHeight{
    objc_setAssociatedObject(self, @selector(imgHeight), imgHeight, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSNumber *)imgY{
    return objc_getAssociatedObject(self, @selector(imgY));
}

- (void)setImgY:(NSNumber *)imgY{
    objc_setAssociatedObject(self, @selector(imgY), imgY, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
